//
// Created by yhy on 17-7-16.
//

#include "MyHandler.h"
#include "Poco/Net/SocketAcceptor.h"
#include "MyReactorServer.h"

uint16_t MyReactorServer::getTimeout() const {
    return reactorRun.timeout;
}

void MyReactorServer::setTimeout(uint16_t timeout) {
    reactorRun.timeout = timeout;
}

uint16_t MyReactorServer::getPort() const {
    return reactorRun.port;
}

void MyReactorServer::setPort(uint16_t port) {
    reactorRun.port = port;
}

void MyReactorServer::start() {

    //===============启动线程======================
    thread.start( reactorRun );
}

void MyReactorServer::stop() {
    reactorRun.reactor.stop();
    thread.join();
}

void MyReactorServer::ReactorRun::run() {
    //===============初始化 reactor==================
    Poco::Net::ServerSocket serverSocket(port);

    Poco::Timespan ti( timeout, 0 );
    reactor.setTimeout(ti);
    Poco::Net::SocketAcceptor <MyHandler> acceptor(serverSocket, reactor);

    std::cout << "run reactor" << std::endl;
    reactor.run();
    std::cout << "stop reactor" << std::endl;
}
